using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;

using IGlobalist.Cms.Utils;

namespace IGlobalist.Cms.Model
{
    [Serializable]
    public class BaseProperty : IContentProperty
    {
        #region Members
        private string _Name;

        public string Name
        {
            get { return _Name; }
            set { _Name = value; }
        }
        private string _DisplayName;

        public string DisplayName
        {
            get { return _DisplayName; }
            set { _DisplayName = value; }
        }
        private string _Description;

        public string Description
        {
            get { return _Description; }
            set { _Description = value; }
        }
        private object _Value;

        public object Value
        {
            get { return _Value; }
            set { _Value = value; }
        }
        private DataType _Type;

        public DataType Type
        {
            get { return _Type; }
            set { _Type = value; }
        }
        private InputType _InputType;

        public InputType InputType
        {
            get { return _InputType; }
            set { _InputType = value; }
        }

        private PropertyType _PropertyType = PropertyType.Extention;
        public PropertyType PropertyType
        {
            get { return _PropertyType; }
            set { _PropertyType = value; }
        }

        #endregion

        #region Override
        public override bool Equals(object obj)
        {
            if (obj is BaseProperty)
            {
                return ((BaseProperty)obj).Name.Equals(this._Name);
            }
            return false;
        }
        public override int GetHashCode()
        {
            return (_Name + _Type + _InputType).GetHashCode();
        }
        public override string ToString()
        {
            string template = "[Name: {0} ,DisplayName: {1}, Description: {2} , Value: {3}, Type: {4}, InputType: {5},PropertyType{6}]";
            return string.Format(template, _Name, _DisplayName, _Description, _Value, Type, InputType,PropertyType);
        }
        #endregion

        #region Utilities
        public string ToXmlString()
        {
            string xml = "";
            XmlNode elm = (XmlNode)(new XmlDocument()).CreateElement("property");
            XmlHelper.CreateAttribute(elm, "Name", _Name);
            XmlHelper.CreateAttribute(elm, "DisplayName", _DisplayName);
            XmlHelper.CreateAttribute(elm, "Description", _Description);
            XmlHelper.CreateAttribute(elm, "Value", _Value.ToString());
            XmlHelper.CreateAttribute(elm, "Type", _Type.ToString());
            XmlHelper.CreateAttribute(elm, "InputType", _InputType.ToString());
            XmlHelper.CreateAttribute(elm, "PropertyType", _PropertyType.ToString());
            
            xml = elm.OuterXml;
            return xml;
        }
        public bool FromXml(string xml)
        {
            try
            {
                XmlDocument xdoc = new XmlDocument();
                xdoc.InnerXml = xml;
                XmlNode root = xdoc.SelectSingleNode("property");
                _Name = XmlHelper.GetAttributeValue(root, "Name");
                _DisplayName = XmlHelper.GetAttributeValue(root, "DisplayName");
                _Description = XmlHelper.GetAttributeValue(root, "Description");
                _Type = (DataType)Enum.Parse(typeof(DataType), XmlHelper.GetAttributeValue(root, "Type"));
                _InputType = (InputType)Enum.Parse(typeof(InputType), XmlHelper.GetAttributeValue(root, "InputType"));
                _PropertyType = (PropertyType)Enum.Parse(typeof(PropertyType), XmlHelper.GetAttributeValue(root, "PropertyType"));
                _Value = XmlHelper.GetAttributeValue(root, "Value");
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }
        #endregion




        
     
    }
  
}
